package org.jeecg.modules.paycenter.service;

import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.paycenter.enums.PayChannelEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class MemberChannelFactory {

    private static ConcurrentHashMap<PayChannelEnum, IMemberChannelService>  _channels= new ConcurrentHashMap <>();

    public MemberChannelFactory(@Autowired List<IMemberChannelService> channelServices) {
        for (IMemberChannelService channelService : channelServices) {
            PayChannelEnum channelEnum = channelService.getChannelEnum();
            if(channelEnum == null){
                throw new JeecgBootException("支付渠道错误");
            }
            if(_channels.containsKey(channelEnum)){
                throw new JeecgBootException("支付渠道重复");
            }
            _channels.put(channelEnum, channelService);
        }
    }

    public static IMemberChannelService getMemberChannel(PayChannelEnum channelEnum){
        IMemberChannelService channelService = _channels.get(channelEnum);
        return channelService;
    }

    public static IMemberChannelService getMemberChannel(String channel){
        PayChannelEnum channelEnum = PayChannelEnum.getChannel(channel);
        IMemberChannelService channelService = _channels.get(channelEnum);
        return channelService;
    }
}
